package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.common.manger.dto.AnnouncementsFindDto;
import com.zhentao.model.common.manger.pojo.Announcements;
import com.zhentao.service.MaAnnouncementsService;
import com.zhentao.mapper.MaAnnouncementsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Set;

/**
* @author 薄超轩
* @description 针对表【announcements】的数据库操作Service实现
* @createDate 2024-09-28 17:13:03
*/
@Service
public class MaAnnouncementsServiceImpl extends ServiceImpl<MaAnnouncementsMapper, Announcements>
    implements MaAnnouncementsService {
    @Resource
    private MaAnnouncementsMapper announcementsMapper;
    @Resource
    private KafkaTemplate<String,Object> kafkaTemplate;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;


    /**
     * 后台管理：公告新增
     * @param announcements
     * @return
     */
    @Override
    public ResponseResult maAnnouncementsInsert(Announcements announcements) {
        if (announcements==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        announcementsMapper.insert(announcements);
        long time = announcements.getPublishTime().getTime();
        long currentTimeMillis = System.currentTimeMillis();
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MINUTE, 5);
        long timeInMillis = instance.getTimeInMillis();
        if(time<=currentTimeMillis){
            kafkaTemplate.send("announcements", JSON.toJSONString(announcements));
        }else if(time<=timeInMillis){
            redisTemplate.opsForZSet().add("announcements",announcements, time);
        }
        return ResponseResult.okResult("新增成功");
    }
    @Scheduled(cron = "0/5 * * * * ?")
    public void checkEndTime(){
        //获取当前时间+0分钟
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MINUTE, 0);
        long timeInMillis = instance.getTimeInMillis();
        Set<Object> announcements = redisTemplate.opsForZSet().rangeByScore("announcements", 0, timeInMillis);
        for (Object o : announcements) {
            kafkaTemplate.send("announcements", JSON.toJSONString(o));
            redisTemplate.opsForZSet().remove("announcements",o);
        }
    }

    /**
     * 后台管理：公告删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult maAnnouncementsDelete(Integer id) {
        Announcements announcements=announcementsMapper.selectById(id);
        if(announcements.getStatus()==1){
            return ResponseResult.okResult(500,"该公告已发布，无法删除");
        }
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        announcementsMapper.deleteById(id);
        return ResponseResult.okResult("删除成功");
    }

    /**
     * 后台管理：公告修改
     * @param announcements
     * @return
     */
    @Override
    public ResponseResult maAnnouncementsUpdate(Announcements announcements) {
        if(announcements.getStatus()==1){
            return ResponseResult.okResult(500,"该公告已发布，无法修改");
        }
        if (announcements==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        announcementsMapper.updateById(announcements);
        return ResponseResult.okResult("修改成功");
    }

    /**
     * 后台管理：公告查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult maAnnouncementsFindAll(AnnouncementsFindDto dto) {
        Page<Announcements>page=new Page<>(dto.getPage(),dto.getSize());
        QueryWrapper<Announcements> wrapper=new QueryWrapper<>();
        wrapper.lambda().like(StringUtils.isNotBlank(dto.getName()), Announcements::getTitle, dto.getName());
        Page<Announcements> page1 = announcementsMapper.selectPage(page, wrapper);
        return ResponseResult.okResult(page1);
    }

    @Override
    public ResponseResult upAndDown(Integer id) {
        Announcements announcements=announcementsMapper.selectById(id);
        if(announcements.getStatus()==1){
            announcements.setStatus(2);
            announcementsMapper.updateById(announcements);
            return ResponseResult.okResult("已启用");
        }else{
            announcements.setStatus(1);
            announcementsMapper.updateById(announcements);
            return ResponseResult.okResult("已禁用");
        }
    }
}




